﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MVCms.Core.Developers;
using MVCms.Core.Entities;
using MVCms.Core.Services;


namespace Plugin.BreadCrumb.Controllers
{
    public class BreadCrumbController : BaseController
    {
        [PluginModule(Name = "BreadCrumb", Description = "BreadCrumb", DisplayName = "BreadCrumb")]
        public PartialViewResult BreadCrumb()
        {
            StringBuilder breadcrumb = new StringBuilder();
            breadcrumb.Append("<a href=/>Home</a>");

            foreach (string url in ControllerContext.HttpContext.Request.RawUrl.ToString().Split('/'))
            {
                VirtualPage page = PageService.GetPage(url);

                if (page != null)
                {
                    if (url.Length > 0 && page.Name != "Home")
                        breadcrumb.Append(string.Format(@" \ <a href={0}>{1}</a>", page.Url, page.Name));
                }
            }

            return PartialView(new BreadCrumbViewModel{BreadCrumb = breadcrumb.ToString()});
        }

        public class BreadCrumbViewModel
        {
            public string BreadCrumb { get; set; }
        }
    }
}
